package com.example.homeworkhelper.menu;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.homeworkhelper.R;
import com.example.homeworkhelper.mainPage.MainPageActivity;
import com.example.homeworkhelper.userInfo.UserInfoActivity;


public class MainActivity extends TabActivity {

    private TabHost tabHost;
    private static int REQUEST_GPS = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost = getTabHost();

        addTab("首页",R.drawable.tab_camara_selector,MainPageActivity.class );
        addTab("个人",R.drawable.user_selector,UserInfoActivity.class);
        requestPermissions();
    }

//    public void addTab(String tag, String title, Class clazz, Drawable drawable){
//        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
//
//        Intent intent = new Intent(getApplicationContext(), clazz);
//        tabSpec.setContent(intent);
//        tabSpec.setIndicator(title,  drawable);
//
//        tabHost.addTab(tabSpec);
//
//    }

    private void addTab(String label, int drawableId, Class clazz) {
        Intent intent = new Intent(this, clazz);
        TabHost.TabSpec spec = tabHost.newTabSpec(label);
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(label);
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(drawableId);
        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }

    @Override
    protected void onStop(){
        super.onStop();
    }
    private void requestPermissions(){
        //REQUEST_GPS为自定义int型静态常量；private final int REQUEST_GPS = 1;
        //申请动态权限，这里的权限申请绑定在了全局控件
        int camerCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);//检查是否有权限
        int readCheck = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_EXTERNAL_STORAGE);//检查是否有权限
        int writeCheck = ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE);//检查是否有权限
        if(camerCheck== PackageManager.PERMISSION_DENIED||readCheck==PackageManager.PERMISSION_DENIED||writeCheck==PackageManager.PERMISSION_DENIED){
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.CAMERA,
                            Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS
                    }
                    , REQUEST_GPS);
        }
    }
}
